package cn.pzx.main_module.ui.fragment

import android.os.Bundle
import android.view.View
import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import cn.pzx.base_module.base.BaseFragment
import cn.pzx.base_module.base.BaseWebUrlActivity
import cn.pzx.base_module.data.ListProjectBean
import cn.pzx.base_module.utils.GlideUtils
import cn.pzx.main_module.R
import cn.pzx.main_module.databinding.FragmentMainLayoutBinding
import cn.pzx.main_module.databinding.ViewFragmentMainHeadBinding
import cn.pzx.main_module.ui.adapter.BannerImageAdapter
import cn.pzx.main_module.ui.vm.MainFragmentVM
import com.blankj.utilcode.util.ActivityUtils
import com.chad.library.adapter.base.BaseQuickAdapter
import com.chad.library.adapter.base.listener.OnItemClickListener
import com.chad.library.adapter.base.viewholder.BaseViewHolder
import com.scwang.smart.refresh.footer.ClassicsFooter
import com.scwang.smart.refresh.header.ClassicsHeader
import com.youth.banner.indicator.CircleIndicator


/**
 * @author pzx
 * @description:
 * @date :3/4/21 3:04 PM
 */
class MainFragment : BaseFragment<MainFragmentVM, FragmentMainLayoutBinding>() {


    var name: String = "test"
    var page = 0
    var articleListApapter: MainArticleAdapter? = null
    var list: ArrayList<ListProjectBean.Article>? = null

    var headView: View? = null

    override fun initVM() {
        vm.articleList.observe(this, Observer {
            vb.refreshLayout.finishRefresh()
            vb.refreshLayout.finishLoadMore()
            if (page == 1) list!!.clear()

            it!!.datas?.let { it1 -> list!!.addAll(it1) }
            articleListApapter!!.notifyDataSetChanged()

        })
        vm.errorData.observe(this,{
            vb.refreshLayout.finishRefresh()

        })


    }

    fun initHeadView() {
        var headBinding = ViewFragmentMainHeadBinding.inflate(layoutInflater)

        headView = headBinding.root

        var bannerAdapter = BannerImageAdapter(vm.imageUrls)
        headBinding.banner?.let {
            it.addBannerLifecycleObserver(this)
            it.setIndicator(CircleIndicator(mContext))
            it.setBannerGalleryMZ(20, 0.8f)
            it.setBannerRound(20f)
            it.adapter = bannerAdapter

        }
        bannerAdapter.setOnBannerListener { data, position ->
            ActivityUtils.startActivity(Bundle().apply {
                putString("url", "https://baidu.com")
            }, BaseWebUrlActivity::class.java)
        }


    }

    override fun initView() {
        initHeadView()
        vb.refreshLayout.setRefreshHeader(ClassicsHeader(mContext))
        vb.refreshLayout.setRefreshFooter(ClassicsFooter(mContext))
        list = ArrayList()
        vm.getAriticleList(++page)
        vb.tvTitle.text = name
        articleListApapter =
            MainArticleAdapter(
                R.layout.item_fragment_main,
                list
            )
        vb.rvFragmentMain.adapter = articleListApapter
        articleListApapter!!.setOnItemClickListener(OnItemClickListener({ adapter, view, position ->
            ActivityUtils.startActivity(Bundle().apply {
                this.putString(
                    "url",
                    (adapter.getItem(position) as ListProjectBean.Article).link
                )
            }, BaseWebUrlActivity::class.java)
        }))

        vb.rvFragmentMain.layoutManager = LinearLayoutManager(mContext)


        vb.refreshLayout.setOnRefreshListener {//下拉刷新
            page = 1
            vm.getAriticleList(page)
        }
        vb.refreshLayout.setOnLoadMoreListener {//上拉加载
            vm.getAriticleList(++page)
        }

        headView?.let { articleListApapter!!.addHeaderView(it) }

    }

    override fun initClick() {


    }

    override fun initData() {
    }

    class MainArticleAdapter(layoutResId: Int, data: MutableList<ListProjectBean.Article>?) :
        BaseQuickAdapter<ListProjectBean.Article, BaseViewHolder>(layoutResId, data) {
        override fun convert(holder: BaseViewHolder, item: ListProjectBean.Article) {

            holder.setText(R.id.tv_title, item.title)
            holder.setText(R.id.tv_desc, item.desc)
            GlideUtils.loadImage(context, item.envelopePic!!, holder.getView(R.id.iv_pic))


        }


    }


}